package com.atguigu.lease.web.admin.interceptors;

import com.atguigu.lease.common.Advice.LeaseException;
import com.atguigu.lease.common.jwt.JwtUtil;
import com.atguigu.lease.common.result.ResultCodeEnum;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.util.ObjectUtils;
import org.springframework.web.servlet.HandlerInterceptor;

//创建登录的拦截器
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String accessToken = request.getHeader("access_token");//获取请求头中的token
        if (ObjectUtils.isEmpty(accessToken))//判断token是否为空
        {
            throw new LeaseException(ResultCodeEnum.ADMIN_LOGIN_AUTH);
        }else{
            //有token 判断是否过期
            JwtUtil.parseToken(accessToken);
            return true;
        }
    }
}